package com.carlinfo.common.util;

import com.alibaba.fastjson.JSONObject;

public class PageInfoUtil
{
	/* 总条数 */
	private int totalRecord;
	/* 每页多少个 */
	private int pageSize = 10;
	/* 当前页 */
	private int currentPage;
	/* 总页数 */
	private int totalPage;
	/* 上一页 */
	private int prePage;
	/* 下一页 */
	private int nextPage;
	/*
	 * 当前的记录数 每页的起始条数; 每页10条; 1 1 2 11 3 21 4 31
	 */
	private int currentRecord;

	public int getTotalRecord()
	{
		return totalRecord;
	}

	public void setTotalRecord(int totalRecord)
	{
		this.totalRecord = totalRecord;
	}

	public int getPageSize()
	{
		return pageSize;
	}

	public void setPageSize(int pageSize)
	{
		this.pageSize = pageSize;
	}
	
	/**
	 * currentPage:-1;3002328382383
	 * @return
	 */
	public int getCurrentPage()
	{
		/* 当前页小于1 */
		if(this.currentPage < 1 )
		{
			this.currentPage = 1 ;
		}
		/* 当前页大于总页数 */
		if(this.getTotalRecord() >= 0 && 
				this.currentPage > this.getTotalPage())
		{
			this.currentPage = this.getTotalPage(); 
		}
		
		/* 如果当前页为0;就变成1 */
		if(this.currentPage == 0 )
		{
			this.currentPage = 1 ; 
		}
		return currentPage;
	}

	public void setCurrentPage(int currentPage)
	{
		this.currentPage = currentPage;
	}
	
	/**
	 * 总页数咋算出来的
	 * 总条数:30;每页:10条
	 * 总页数:30 / 10 == 3;
	 * 总条数:35;每页:10条
	 * 总页数:30 / 10 + 1== 3;
	 * @return
	 */
	public int getTotalPage()
	{
		if(this.totalRecord % this.pageSize == 0 )
		{
			this.totalPage = this.totalRecord / this.pageSize ; 
		}else
		{
			this.totalPage = this.totalRecord / this.pageSize + 1; 
		}
		return totalPage;
	}

	public int getPrePage()
	{
		prePage = this.currentPage - 1 ; 
		if(this.prePage < 1 )
		{
			this.prePage = 1 ;
		}
		return prePage;
	}

	public int getNextPage()
	{
		this.nextPage = this.currentPage + 1;
		/* 当前页大于总页数 */
		if(this.getTotalRecord() >= 0 
				&& this.nextPage > this.getTotalPage())
		{
			this.nextPage = this.getTotalPage(); 
		}
		
		if(this.nextPage == 0 )
		{
			this.nextPage = 1 ; 
		}
		return nextPage;
	}
	
	/**
	 * 当前页的起始条数;每页10条
	 * 当前页	起始条数
	 * 1	1
	 * 2	11
	 * 3	21
	 * 4	31
	 * 5	51
	 * 起始条数如何算出来的:(当前页 - 1) * 每页多少条 + 1 ;
	 * @return
	 */
	public int getCurrentRecord()
	{
		this.currentRecord = (this.getCurrentPage() - 1) * this.getPageSize();  
		return currentRecord;
	}
	
	/**
	 * 
{
	"currentPage":"1",
	"pageSize":"10",
	"totalRecord":"50",
	"totalPage":"20",
	"prePage":"10",
	"nextPage":"11",
	"currentRecord","11"
}
	 * @return
	 */
	public String toJSON()
	{
		JSONObject resultJSON = new JSONObject() ; 
		/* 拼装JSON */
		resultJSON.put("currentPage", this.getCurrentPage() + "");
		resultJSON.put("pageSize", this.getPageSize() + "");
		resultJSON.put("totalRecord", this.getTotalRecord() + "");
		resultJSON.put("totalPage", this.getTotalPage() + "");
		resultJSON.put("prePage", this.getPrePage() + "");
		resultJSON.put("nextPage", this.getNextPage() + "");
		resultJSON.put("currentRecord", this.getCurrentRecord() + "");
		return resultJSON.toJSONString() ; 
	}
	
	public static void main(String[] args)
	{
		/**
		 * 共有三种情况测试
		 * ~所有的都正常
		 * ~总条数是0,总页数,也是0;当前页的判断
		 * ~总条数和总页数正常,当前页不正常(当前页<0,当前页>总页数)
		 */
		PageInfoUtil pageInfoUtil = new PageInfoUtil() ; 
		/* 设置总条数 */
		pageInfoUtil.setTotalRecord(0);
		/* 设置一下当前页 */
		pageInfoUtil.setCurrentPage(6);
		/* 设置一下每页多少条 */
		pageInfoUtil.setPageSize(10);
		
		System.out.println("=====>" + pageInfoUtil.toJSON());
	}
}
